package org.jwcloud.project.module.zx.common.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.jwcloud.project.module.zx.baseManager.service.repository.MatBaseManagerRepository;
import org.jwcloud.project.module.zx.common.FmtUtil;
import org.jwcloud.project.module.zx.common.NbSqlFactory;
import org.jwcloud.project.module.zx.common.Resp;
import org.jwcloud.project.module.zx.common.importHelper.BaseExcelHandle;
import org.jwcloud.project.module.zx.common.importHelper.DataStore;
import org.jwcloud.project.module.zx.common.importHelper.ExcelConfig;
import org.jwcloud.project.module.zx.common.importHelper.IBaseExcel;
import org.jwcloud.project.module.zx.common.repository.RptCommonRepository;
import org.jwcloud.project.module.zx.common.service.RptUtService;
import org.jwcloud.project.module.zx.txzldp.service.repository.TxzldpCommonViewRepository;
import org.jwcloud.project.module.zx.workTime.service.repository.MatWorkTimeRepository;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.*;

@Api(value = "API - Controlle",
description = "test", tags = "test")
@RestController
@Transactional
@RequestMapping("/rest/ytest/Test")
public class TestController {
	
	@Autowired
	public NbSqlFactory nbSqlFactory;
	
	@Autowired
    private SqlSessionTemplate sqlSessionTemplate;
	
	@GetMapping("test")
    @ApiOperation(value = "修改时效", notes = "")
    @ApiImplicitParams({
    	
    })
    public Resp updateTipTime(
    		@RequestBody(required = false) Map<String,String> param
    		) throws Exception{
		nbSqlFactory.run();
		return Resp.success(1);
    }
	
	@RequestMapping("/testView")
	public ModelAndView testView(String param, ModelAndView model) {
		
		System.out.println("sqlSessionTemplate； " + sqlSessionTemplate);
		
		model.setViewName("pages/test/Test.html");
		return model;
	}
	
    @RequestMapping("/uploadTemplateForm")
    @ResponseBody
    public String uploadTemplateForm(HttpServletRequest request, MultipartFile xlsTemplate) throws Exception {
    	
		if(1 == 11)
			return null;
		
		long s1 = System.currentTimeMillis();
		
    	ExcelConfig excelConfig = new ExcelConfig(xlsTemplate, 0, 1, -1, 0, 5, "test.xls_test");
		IBaseExcel ii = new BaseExcelHandle() {
			
			@Override
			protected int checkRow(Object[] row, DataStore data) {
				// TODO Auto-generated method stub
				System.out.println("外部打印：" + Arrays.toString(row));
				row[0] = null;
				data.setData(row);
				return 1;
			}

			@Override
			protected void beforeHandle() {
				// TODO Auto-generated method stub
			}

			@Override
			protected void afterHandle() {
				// TODO Auto-generated method stub
			}
			
		};
		
		ii.run(excelConfig);
		System.out.println("s1: " + s1);
    	
        return "1";//回调函数，调用前台的方法
    }
    
    @RequestMapping("/uploadTemplateForm_wz")
    @ResponseBody
    public String uploadTemplateForm_wz(HttpServletRequest request, MultipartFile xlsTemplate) throws Exception {
    	
    	ExcelConfig excelConfig = new ExcelConfig(xlsTemplate, 0, 1, -1, 0, 5, "test.wz_test");
    	excelConfig.setBatchSize(200);
    	
    	long s1 = System.currentTimeMillis();
    	
		IBaseExcel ii = new BaseExcelHandle() {
			
			@Override
			protected int checkRow(Object[] row, DataStore data) {
				// TODO Auto-generated method stub
//				System.out.println("外部打印：" + Arrays.toString(row));
				data.setData(row);
				return BaseExcelHandle.GO_CONTINUE;
			}

			@Override
			protected void beforeHandle() {
				// TODO Auto-generated method stub
				
			}

			@Override
			protected void afterHandle() {
				// TODO Auto-generated method stub
				System.out.println("s2: " + System.currentTimeMillis());
			}
			
		};
		
		ii.run(excelConfig);
		System.out.println("s1: " + s1);
    	
        return "1";//回调函数，调用前台的方法
    }
	
}